<!-- <template>
  <div id="login">
  <el-form :model="loginForm" status-icon :rules="rules" ref="loginForm" label-width="100px" class="demo-loginForm">
  <el-form-item label="账号" prop="username">
    <el-input v-model="loginForm.username" autocomplete="off"></el-input>
  </el-form-item>
  <el-form-item label="密码" prop="password">
    <el-input type="password" v-model="loginForm.password" autocomplete="off"></el-input>
  </el-form-item>
  <el-form-item>
    <el-button type="primary" @click="submitForm('loginForm')">提交</el-button>
    <el-button @click="resetForm('loginForm')">重置</el-button>
    <el-button @click="goRegister">去注册</el-button>
  </el-form-item>
</el-form>
</div>
</template> -->
<template>
  <div class="login-wrap">
      <div class="ms-login">
          <div class="ms-title">订餐管理系统</div>
          <el-form :model="loginForm" :rules="rules" ref="loginForm" label-width="0px" class="ms-content">
              <el-form-item prop="username">
                  <el-input v-model="loginForm.username" placeholder="username">
                      <el-button slot="prepend" icon="el-icon-lx-people"></el-button>
                  </el-input>
              </el-form-item>
              <el-form-item prop="password">
                  <el-input type="password" placeholder="password" v-model="loginForm.password"
                      @keyup.enter.native="submitForm('loginForm')">
                      <el-button slot="prepend" icon="el-icon-lx-lock"></el-button>
                  </el-input>
              </el-form-item>
              <div class="login-btn">
                  <el-button type="primary" @click="submitForm('loginForm')">登录</el-button>
                  <el-button @click="goRegister">去注册</el-button>
              </div>
          </el-form>
      </div>
  </div>
</template>

<script>
  export default {
    data() {
      var validateUsername = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入密码'));
        } else {
          if (this.loginForm.checkPass !== '') {
            this.$refs.loginForm.validateField('checkPass');
          }
          callback();
        }
      };
      var validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入密码'));
        } else {
          callback();
        }
      };
      return {
        loginForm: {
          username: '',
          password: '',
        },
        rules: {
          username: [
            { validator: validateUsername, trigger: 'blur' }
          ],
          checkPass: [
            { validator: validatePass, trigger: 'blur' }
          ],
        }
      };
    },
    methods: {
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          console.log(this.loginForm);
          this.$axios.get("http://localhost:8082/doLogin",
                    {
                        params: {
                          username:this.loginForm.username,
                          password:this.loginForm.password,
                        }
                    }
                )
          .then(response =>{
            console.log(response);
            if(response.data.code!='200'){
              this.$message.error("登录失败：请检查账号密码是否正确");
            }else{
              this.$message("登录成功");
              localStorage.setItem("auth",response.data.data);
              this.$router.push("/main");
            }
          })
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      },
      goRegister(){
        this.$router.push("/register")
      }
    }
  }
</script>
<style scoped>
  .login-wrap {
      position: relative;
      width: 100%;
      height: 100%;
      background-image: url(../assets/loginimg.jpeg);
      background-size: 100%;
  }
  .ms-login{
    width: 380px;
    position: relative;
    left: 35%;
  }
  .ms-title {
      width: 100%;
      line-height: 50px;
      text-align: center;
      font-size: 20px;
      color: #000;
      border-bottom: 1px solid #ddd;
  }


  .ms-content {
      padding: 30px 30px;
  }

  .login-btn {
      text-align: center;
  }
  
  .login-btn button {
      width: 100%;
      height: 36px;
      margin-bottom: 10px;
  }

  .login-tips {
      font-size: 12px;
      line-height: #000;
  } 
</style>